import { defineStore } from "pinia";
import { loginApi } from "@/api/user";
import { setToken,getToken ,setUserId,getUserId, removeToken,  getUserRole, setUserInfo, removeUserInfo} from "@/utils/token";

export const useUserStore = defineStore('user',{
  state: () => ({
    token:getToken() || '',
    userId: getUserId() || '',
    userRole:getUserRole() || ''
  }),

  // 数据修改的函数
  actions: {
    // 设置token
    setToken(token) {
      this.token = token
      setToken(token)
    },
    setUserId(id) {
      this.userId = id
      setUserId(id)
    },
    setUserInfo(userInfo) {
      this.userRole = userInfo.accountRole
      setUserInfo(userInfo)
    },
    logout() {
      this.token = '',
      this.userId = '',
      this.userRole = '',
      removeToken(),
      removeUserInfo()
    },
    async loginAction(loginForm) {
      setTimeout(() => {
        console.log(111);
      },2000)
      const resp = await loginApi(loginForm)

      const {token} = resp
      this.setToken(token)

      this.setUserInfo(resp.user)
    }
  }
})
